﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace UserControls
{
    public class DatePicker: Panel
    {
        private DropDownList DY;
        private DropDownList DM;
        private DropDownList DD;
        private DateTime fDate=DateTime.Now;        
        private DateTime fMinDate = DateTime.Now.AddYears(-5);
        private DateTime fMaxDate = DateTime.Now.AddYears(5);

        public DatePicker()
        {
            CreateControls();
            SetControls(true);
            SetDateToControls();
        }

        DatePicker(DateTime iDefDate)
        {         
            fDate = iDefDate;
            CreateControls();
            SetControls(true);
            SetDateToControls();
        }

        void CreateControls()
        {
            DY = new DropDownList();
            DY.Style["Position"] = "Relative";
            DY.Style["Float"] = "Left";
            DY.Style["Margin-Top"] = "10px";
            DY.Style["Margin-Left"] = "10px";
            DY.Style["Margin-Right"] = "0px";
            DY.Style["Margin-Bottom"] = "10px";            
            DY.TextChanged += DLChange;
            DY.AutoPostBack = true;

            DM = new DropDownList();            
            DM.Style["Position"] = "Relative";
            DM.Style["Float"] = "Left";
            DM.Style["Margin-Top"] = "10px";
            DM.Style["Margin-Left"] = "0px";
            DM.Style["Margin-Right"] = "0px";
            DM.Style["Margin-Bottom"] = "10px";
            DM.TextChanged += DLChange;
            DM.SelectedIndexChanged += DLChange;
            DM.AutoPostBack = true;

            DD = new DropDownList();            
            DD.Style["Position"] = "Relative";
            DD.Style["Float"] = "Left";
            DD.Style["Margin-Top"] = "10px";
            DD.Style["Margin-Left"] = "0px";
            DD.Style["Margin-Right"] = "10px";
            DD.Style["Margin-Bottom"] = "10px";
            DD.TextChanged += DLChange;
            DD.AutoPostBack = true;

            this.Controls.Add(DY);
            this.Controls.Add(DM);
            this.Controls.Add(DD);
        }

        void SetControls(bool iFull)
        {
            if (iFull)
            {
                DY.Items.Clear();
                for (int I = fMinDate.Year; I <= fMaxDate.Year; I++)
                {
                    DY.Items.Add(I.ToString());
                }

                DM.Items.Clear();
                for (int I1 = 1; I1 <= 12; I1++)
                {
                    DM.Items.Add(I1.ToString());
                }
            }

            DD.Items.Clear();
            int DMax=30;

            if (fDate.Month==1 || fDate.Month==3 || fDate.Month==5 || fDate.Month==7 || fDate.Month==8 ||
                fDate.Month==10 || fDate.Month==12)
            {
                DMax=31;
            }
            if (fDate.Month == 2)
            {
                if (fDate.Year % 400 != 0 && fDate.Year % 4 == 0)
                {
                    DMax = 29;
                }
                else
                {
                    DMax = 28;
                }
            }
            for (int I2 = 1; I2 <= DMax; I2++)
            {
                DD.Items.Add(I2.ToString());
            }
        }

        private void SetControlsToDate()
        {
            fDate = DateTime.Parse(DY.SelectedValue + "." + DM.SelectedValue + "." + DD.SelectedValue);
        }

        private void SetDateToControls()
        {
            DY.SelectedValue = fDate.Year.ToString();
            DM.SelectedValue = fDate.Month.ToString();
            DD.SelectedValue = fDate.Day.ToString();
        }

        private void DLChange(object Sender, EventArgs E)
        {
            //fDate = DateTime.Parse(DY.SelectedValue + "." + DM.SelectedValue + "." + DD.SelectedValue);
            SetControlsToDate();
            if (Sender == DM)
            {
                SetControls(false);
            }
        }

        ////////////////////////
        // properties
        ////////////////////////

        public DateTime MinDate
        {
            get { return fMinDate; }
            set
            {
                if (value != fMinDate)
                {
                    fMinDate = value;
                    SetControls(true);
                }
            }
        }

        public DateTime MaxDate
        {
            get { return fMaxDate; }
            set
            {
                if (value != fMaxDate)
                {
                    fMaxDate = value;
                    SetControls(true);
                }
            }
        }

        public DateTime Date
        {
            get 
            {                
                SetControlsToDate();
                return fDate; 
            }
            set
            {
                if (value != fDate && fDate>=MinDate && fDate<=MaxDate)
                {
                    fDate = value;
                    SetDateToControls();
                }
            }
        }        
    }
}
